local skel = fk.CreateSkill{
  name = "tea__yanzhu",
}
--local U = require "packages/utility/utility"
Fk:loadTranslationTable{
  ["tea__yanzhu"] = "宴诛",
  [":tea__yanzhu"] = "出牌阶段限一次，你可以将一张【杀】当【五谷丰登】使用。此牌结算结束后，你可以对一名装备区牌数不小于你的角色造成1点伤害。若如此做，除其外得到基本牌或普通锦囊牌的角色可以对其使用一张伤害牌。",
  ["#tea__yanzhu-invoke"] = "宴诛：你可以对一名装备区内牌数不小于你的角色造成1点伤害",
  ["#tea__yanzhu-use"] = "宴诛：你可以对 %src 使用一张伤害类牌",
  ["#tea__yanzhu-viewas"] = "宴诛：你可以将一张【杀】当【五谷丰登】使用",
}
---@type ViewAsSkillSpec
local xingxue = {
  mute = true,
  pattern = "amazing_grace",
  prompt = "#tea__yanzhu-viewas",
  card_filter = function (self, player, to_select, selected, selected_targets)
    return #selected == 0 and Fk:getCardById(to_select).trueName == "slash"
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 then return end
    local c = Fk:cloneCard("amazing_grace")
    c:addSubcards(cards)
    c.skillName = skel.name
    ---@diagnostic disable-next-line: assign-type-mismatch
    c.skill = Fk.skills["tea_yanzhu__amazing_grace_skill"]
    return c
  end,
  before_use = function (self, player, use)
    player:broadcastSkillInvoke(skel.name, 1)
    player.room:notifySkillInvoked(player, skel.name, "special")
  end,
  enabled_at_play = function (self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  enabled_at_response = Util.FalseFunc,
}
---@type TrigSkelSpec<UseCardFunc>
local xingxue_delay = {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return player == target and player:hasSkill(skel.name) and data.card and table.contains(data.card.skillNames, skel.name)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player, {
      targets = table.filter(room.alive_players, function (p)
        return #p:getCardIds("e") >= #player:getCardIds("e")
      end),
      min_num = 1,
      max_num = 1,
      skill_name = skel.name,
      prompt = "#tea__yanzhu-invoke"
    })
    if #to == 1 then
      event:setCostData(self, { to = to[1] })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).to ---@type ServerPlayer
    player:broadcastSkillInvoke(skel.name, table.random{2, 3})
    room:notifySkillInvoked(player, skel.name, "offensive", { to })
    room:damage{
      from = player,
      to = to,
      damage = 1,
      skillName = skel.name
    }
    local list
    local phase_id = (room.logic:getCurrentEvent():findParent(GameEvent.Phase) or {}).id
    if phase_id then
      if data.extra_data and data.extra_data.yanzhu_amazing_grace_list then
        list = data.extra_data.yanzhu_amazing_grace_list
      end
    end
    if not player.dead and list then
      local players = {}
      for p, _ in pairs(list) do
        table.insert(players, p)
      end
      if #players > 0 then
        room:sortByAction(players)
        local count = 0
        for _, p in ipairs(players) do
          local cid = list[p]
          if not p.dead and (Fk:getCardById(cid).type == Card.TypeBasic or Fk:getCardById(cid):isCommonTrick())  and not to.dead and p ~= to then
            local card_names = table.filter(Fk:getAllCardNames("bt"), function (n)
              return Fk:cloneCard(n).is_damage_card
            end)
            local pattern = table.concat(card_names, ",")
            local use = room:askToUseCard(p, {
              skill_name = skel.name,
              pattern = pattern,
              prompt = "#tea__yanzhu-use:" .. to.id,
              extra_data = {
                bypass_distances = true,
                bypass_times = true,
                exclusive_targets = { to.id },
              }
            })
            if use then
              if count == 0 then
                player:broadcastSkillInvoke(skel.name, 4)
                room:notifySkillInvoked(player, skel.name, "special", {to})
              end
              count = count + 1
              use.extra_data = use.extra_data or {}
              use.extra_data.tea__xingxue_mark = { to.id }
              room:useCard(use)
            end
          end
        end
      end
    end
  end
}
---@type TrigSkelSpec<UseCardFunc>
local xingxue_target_cancel = {
  can_refresh = function (self, event, target, player, data)
    return player == target and data.extra_data and data.extra_data.tea__xingxue_mark
  end,
  on_refresh = function (self, event, target, player, data)
    local tos = table.map(data.extra_data.tea__xingxue_mark, function (id)
      return player.room:getPlayerById(id)
    end) ---@type ServerPlayer[]
    if #(data.subTos or {}) == 0 then
      data:removeAllTargets()
      data:addTarget(tos)
    else
      local subTo = data.subTos[1]
      data:removeAllTargets()
      data:addTarget(tos, subTo)
    end
  end
}
skel:addEffect("viewas", xingxue)
skel:addEffect(fk.CardUseFinished, xingxue_delay)
skel:addEffect(fk.PreCardUse, xingxue_target_cancel)
-- Fk.generals["mouxusheng"]:addSkill(skel.name)
return skel